<template>
  <el-container class="elContainer">
    <!-- 头部 -->
    <el-header class="elHeader">
      <div>
        <img src="../assets/jingDong.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <!-- 退出按钮 -->
      <el-button type="info" @click="exitButton"> 退出 </el-button>
    </el-header>
    <el-container>
      <!-- 左侧表单 -->
      <el-aside width="200px" class="elAside" >
          <el-menu
            class="el-menu-vertical-demo"
            background-color="#313743"
            text-color="#fff"
            active-text-color="#359bff"
            unique-opened
            :collapse="isJust"
            :collapse-transition='false'
            router
            :default-active="defaultActiveValue"
          >
          <div class="toggle_button" @click="allMenu">|||</div>
          <!-- 一级菜单 -->
            <el-submenu 
            :index="item.id + ''" 
            v-for="item in menuList" 
            :key="item.id"
            >
              <!-- 一级菜单模板 -->
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
             <!--  二级菜单 -->
             <el-menu-item :index="'/'+child.path+''" 
             v-for="child in item.children" :key="child.id"
             @click="setPath('/'+child.path+'')"
             >
               <i class="el-icon-menu"></i>
               <span>{{child.authName}}</span>
               </el-menu-item>
            </el-submenu>
          </el-menu>
      </el-aside>
      <!-- 主要区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "home",
  data(){
    return{
      menuList:[],
      iconsObj:{
        '125':'iconfont icon-yonghuming',
        '103':'iconfont icon-quanxian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-dingdandingdanmingxishouzhimingxi',
        '145':'iconfont icon-bg-data',
      },
      isJust:false,
      defaultActiveValue:''
    }
  },
  created(){
    this.getMenuList()
    this.defaultActiveValue = window.sessionStorage.getItem('defaultActiveValue')
  },
  methods: {
    exitButton() {
      window.sessionStorage.clear("token");
      this.$router.push("/login");
    },
    async getMenuList(){
      const {data} = await this.$http.get('menus');
      if(data.meta.status !== 200) return this.$message.error('data.meta.msg')
      this.menuList = data.data
  },
  allMenu(){
    this.isJust = !this.isJust
  },
  setPath(defaultActiveValue){
      window.sessionStorage.setItem('defaultActiveValue',defaultActiveValue)
  }
  },
};
</script>
<style lang="less" scoped>
.elHeader {
  display: flex;
  justify-content: space-between;
  background-color: #363d40;
  padding-left: 0;
  align-items: center;
  color: aliceblue;
  font-size: 30px;
}
.elHeader span {
  margin-left: 20px;
}
.elHeader img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}
.elAside {
  background-color: #313743;
  .el-menu{
    border-right: none;
  }
}
.elContainer {
  height: 100%;
}
.iconfont{
  margin-right: 10px;
}
.toggle_button{
  background-color: #475163;
  font-size: 15px;
  line-height: 15px;
  text-align: center;
  color: white;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>